<?php 

class Admin_Model_Orders {
	
	/**
	 * @var Doctrine_Query_Abstract
	 */
	protected $query;
	
	protected $_SessionNamespace;
	
	public function __construct(){
		$this->init();
	}
	
	public function init()
    {	
    	$this->loadSessionNamespace();
    }
    
    public function setSessionNamespace(Zend_Session_Namespace $namespace)
	{
		$this->_SessionNamespace = $namespace;
	}
	
	public function getSessionNamespace()
	{
		if(is_null($this->_SessionNamespace)){
			$this->setSessionNamespace(new Zend_Session_Namespace(__CLASS__));
		}
		return($this->_SessionNamespace);
	}
	
	public function persist()
	{
		$this->getSessionNamespace()->query = $this->query;
	}
	
	public function loadSessionNamespace()
	{
		if (isset($this->getSessionNamespace()->query)) {
            $this->query = $this->getSessionNamespace()->query;
        }
	}
	
	public function getQuery(){
		return $this->query;
	}
	
	public function remQuery(){
		$this->query = null;
		$this->persist();
	}
	
	public function create(array $data = array()){
		
		$this->query = Doctrine_Query::create()
							->select()
							->from('Doctrine_Model_Orders o');
							
		
		$id = $data['orderId'];
		$userId = $data['userId'];
		$firstName = $data['firstName'];
		$lastName = $data['lastName'];
		$status = $data['status'];
		$statusArray = Doctrine_Core::getTable('Doctrine_Model_Orders')->getEnumValues('status');
		$statusValue = $statusArray[$status];
		$startDate = ($data['startDate'] != null)?date("Y-m-d H:i:s",strtotime($data['startDate'])):date("Y-m-d H:i:s",0);
		$endDate = ($data['endDate'] != null )?date("Y-m-d H:i:s",strtotime($data['endDate'])):date("Y-m-d H:i:s",time());
							
		if($id)
			$this->query->addWhere("o.id = '$id'");
		if($userId)	
			$this->query->addWhere("o.userId = '$userId'");
			
		$this->query->addWhere("o.status = '$statusValue'");
			
		switch($status){
			case '0':
               	$this->query->addWhere("o.createdOn between '$startDate' and '$endDate'");
               	break;
        	case '1':
            	$this->query->addWhere("o.shippedOn between '$startDate' and '$endDate'");
                break;
            case '2':
            	$this->query->addWhere("o.receivedOn between '$startDate' and '$endDate'");
                break;
            case '3':
				$this->query->addWhere("o.checkedOn between '$startDate' and '$endDate'");
				break;
			case '4':
				$this->query->addWhere("o.checkedOn between '$startDate' and '$endDate'");
				break;
			case '5':
				$this->query->addWhere("o.completedOn between '$startDate' and '$endDate'");
				break;
			case '4':
				$this->query->addWhere("o.completedOn between '$startDate' and '$endDate'");
				break;
			case '5':
				$this->query->addWhere("o.closedOn between '$startDate' and '$endDate'");
				break;
			default:
		}
			
		if($firstName || $lastName){
			$this->query->leftJoin('o.Users u');
			if($firstName)
				$this->query->addWhere("u.firstName = '$firstName'");
			if($lastName)
				$this->query->addWhere("u.lastName = '$lastName'");
		}
		
//		Store query in session;
		$this->persist();
	}
}